package com.foxitcloud.util;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.util.StringUtils;

public class FileUtil  {	
	public final static int write = 0;
	public final static int overwrite = 1;
	public final static int rename = 2;
	
	public final static int File = 0;	
	public final static int Direcory = 1;
	
	public final static String RootPath="/";	
	public final static String RootPathId="0";
	public final static String DirSeprator="/";
	private final static int PATH_LENGTH = 1000;
	
	public static boolean verifyPath(String path) {		
		if (!StringUtils.hasText(path) || path.length() > PATH_LENGTH || path.contains("\\") 
				|| path.contains("?") || path.contains("|") || path.contains("\"")
				|| path.contains(">") || path.contains("<") || path.contains(":")
				|| path.contains("*") || path.contains("\r")|| path.contains("\n")|| path.contains("\t")
				|| path.contains("\0")|| path.contains(String.valueOf((char)0x0B))) {
			return false;
		}
				
		if (!path.startsWith("/")) {
			return false;
		}
		
		String []filesNames = path.split("/");
		for (int i = 1; i < filesNames.length ; i++) {
			if(!StringUtils.hasText(filesNames[i]))
				return false;
		}
		
		return true;
	}

	public static boolean verifyLimit(String limit) {
		if (!StringUtils.hasText(limit))
			return true;
		
		String[] strArray = limit.split("-");
		
		if (strArray.length != 2)
			return false;
		
		if (NumberUtils.isDigits(strArray[0]) && NumberUtils.isDigits(strArray[1])) {
			long start = Long.parseLong(strArray[0]);
			long end = Long.parseLong(strArray[1]);
			if (start <= end) {
				return true;
			}
		}
		
		return false;
	}
	
	public static boolean verifyExtension(List<String> extension) {		
		if (extension == null || extension.isEmpty()) {
			return true;
		} 
		
		for (String ext : extension) {
			if (StringUtils.hasText(ext) && ext.contains(".")) {
				return false;
			}
		}
		
		return true;
	}
	
	public static String getFileNameWithExtension(String name) {	
		if (!StringUtils.hasText(name) || name.endsWith(DirSeprator)) {
			return null;
		}
		
		String [] path =  name.split(FileUtil.DirSeprator);
		if (path.length > 0)
			return path[path.length-1];
		return null;
	}
	
	public static String getFileNameNoExtension(String name) {	
		String file = getFileNameWithExtension(name);
		
		if (StringUtils.hasText(file)) {
			int position = file.lastIndexOf(".");
			if (position <= 0) {
				return file;
			} else {
				return file.substring(0, position);
			}
		}
		
		return file;
	}
	
	public static String getFileExtension(String name) {	
		if (!StringUtils.hasText(name)) {
			return null;
		}
		
		int position = name.lastIndexOf(".");
		if (position <= 0) {
			return null;
		}
				
		return name.substring(position + 1);
	}
	
	public static String getParentDirName(String name) {
		if (!StringUtils.hasText(name)) {
			return RootPath;
		}
		
		name = formatPath(name);
		
		int position = name.lastIndexOf(DirSeprator);
		if (position <= 0) {
			return RootPath;
		}
		
		if (name.endsWith(DirSeprator)) {
			String [] path =  name.split(DirSeprator);
			StringBuilder sb = new StringBuilder();			
			for (int i = 0; i < path.length - 1; i++) {
				String subPath = path[i];
				if (StringUtils.hasText(subPath))
					sb.append(DirSeprator).append(subPath);
			}
			sb.append(DirSeprator);
			
			return sb.toString();
		} else {
			return formatDirName(name.substring(0, position));
		}		
	}
	
	public static String formatDirName(String dir) {
		if (!StringUtils.hasText(dir)) {
			return RootPath;
		}
		
		if (!dir.endsWith(RootPath)) {
			dir += RootPath;
		}
		
		return dir.replaceAll("[/]+", "/");
	}
	
	public static String formatPath(@NotNull String path) {		
		return path.replaceAll("[\\s\0\\x0f|\"*:<>?\\\\]+", "").replaceAll("[/]+","/");
	}
	
	public static boolean isDirectory(String path) {
		if (!StringUtils.hasText(path)) {
			return false;
		}
		
		return path.endsWith(DirSeprator) ? true : false;
	}
}
